#!/usr/bin/env python3  /*设定脚本文件直接能执行*/
# -*- coding:utf-8 -*-   /*设置编码*/
"""
@Author: WANDERSON M.PIMENTA
@Software: PyCharm 、Qt Designer and PySide6
@File: tQMainWindowUI.py
@Time: 2024/04/03 13:32
@Function：ctQMainWindowUI：MainWindow GUI 界面实现
"""
from UI.cores.tFunctions import Functions
from .tQMainWindowWidget import *
from UI.elements.columns.tQRightColumnUI import ctQRightColumnUI
from UI.elements.pages.tQMainPages import ctQMainPages
from UI.widgets.tQCopyRightVersionBar.tQCopyRightVersionBar import ctQCopyRightVersionBar


# MainWindow布局
class ctQMainWindowUI(object):
    # 初始化
    def __init__(self):
        """
        MainWindow GUI 界面实现
        """
        # 标题栏 & 窗口
        self.titleBar = None
        self.window = None
        # 窗体的父控件布局
        self.layoutCentralWidget = None
        # 窗体的父控件部件
        self.widgetCentral = None
        # 应用程序设置
        self.settings = None
        # 主题
        self.themes = None

    # 布局函数
    def f_mainFormLayout(self, parent):

        # <editor-fold desc="设置objectName & 加载设置文件 & 加载主题 & 设置大小">
        # 设置objectName
        if not parent.objectName():
            parent.setObjectName("MainWindow")

        # 加载设置文件（加载Json文件）
        settings = Settings()
        self.settings = settings.items

        # 加载主题
        themes = Themes()
        self.themes = themes.items

        # 设置大小
        parent.resize(self.settings["startup_size"][0], self.settings["startup_size"][1])
        parent.setMinimumSize(self.settings["minimum_size"][0], self.settings["minimum_size"][1])
        # </editor-fold>

        # <editor-fold desc="窗体的父控件">
        # 窗体的父控件
        self.widgetCentral = QWidget()
        # 窗体的父控件设置样式
        self.widgetCentral.setStyleSheet(f'''
            font: {self.settings["font"]["text_size"]}pt "{self.settings["font"]["family"]}";
            color: {self.themes["app_color"]["text_foreground"]};
        ''')
        # 窗体的父控件布局
        self.layoutCentralWidget = QVBoxLayout(self.widgetCentral)
        # 从设置文件中判断是否有标题栏
        if self.settings["custom_title_bar"]:
            self.layoutCentralWidget.setContentsMargins(10, 10, 10, 10)
        else:
            self.layoutCentralWidget.setContentsMargins(0, 0, 0, 0)
        # </editor-fold>

        # <editor-fold desc="窗体">
        # 窗体
        self.window = ctQWindow(parent,
                                bg_color=self.themes["app_color"]["bg_one"],
                                border_color=self.themes["app_color"]["bg_two"],
                                text_color=self.themes["app_color"]["text_foreground"]
                                )
        # 如果禁用自定义标题栏
        if not self.settings["custom_title_bar"]:
            self.window.f_setStylesheet(border_radius=0, border_size=0)

        # 窗体的父控件添加窗体
        self.layoutCentralWidget.addWidget(self.window)
        # </editor-fold>

        # <editor-fold desc="左侧菜单栏">
        # 左侧菜单栏外边距&最小尺寸
        __m_leftMenuBarMargin = self.settings["left_menu_content_margins"]
        __m_leftMenuBarMinimum = self.settings["lef_menu_size"]["minimum"]

        # 左侧菜单栏框架
        self.frameLeftMenuBar = QFrame()
        self.frameLeftMenuBar.setMaximumSize(__m_leftMenuBarMinimum + (__m_leftMenuBarMargin * 2), 17280)
        self.frameLeftMenuBar.setMinimumSize(__m_leftMenuBarMinimum + (__m_leftMenuBarMargin * 2), 0)

        # 左侧菜单栏布局
        self.__m_layoutLeftMenuBar = QHBoxLayout(self.frameLeftMenuBar)
        self.__m_layoutLeftMenuBar.setContentsMargins(__m_leftMenuBarMargin,
                                                      __m_leftMenuBarMargin,
                                                      __m_leftMenuBarMargin,
                                                      __m_leftMenuBarMargin
                                                      )

        # 左侧菜单栏
        self.leftMenuBar = ctQLeftMenuBar(parent=self.frameLeftMenuBar,
                                          # For tooltip parent
                                          app_parent=self.widgetCentral,
                                          dark_one=self.themes["app_color"]["dark_one"],
                                          dark_three=self.themes["app_color"]["dark_three"],
                                          dark_four=self.themes["app_color"]["dark_four"],
                                          bg_one=self.themes["app_color"]["bg_one"],
                                          icon_color=self.themes["app_color"]["icon_color"],
                                          icon_color_hover=self.themes["app_color"]["icon_hover"],
                                          icon_color_pressed=self.themes["app_color"]["icon_pressed"],
                                          icon_color_active=self.themes["app_color"]["icon_active"],
                                          context_color=self.themes["app_color"]["context_color"],
                                          text_foreground=self.themes["app_color"]["text_foreground"],
                                          text_active=self.themes["app_color"]["text_active"],
                                          toggle_text="隐藏菜单",
                                          toggle_tooltip="显示菜单"
                                          )
        # 左侧菜单栏布局添加控件
        self.__m_layoutLeftMenuBar.addWidget(self.leftMenuBar)
        # </editor-fold>

        # <editor-fold desc="左列">
        # 左列框架
        self.frameLeftColumn = QFrame()
        self.frameLeftColumn.setMaximumWidth(self.settings["left_column_size"]["minimum"])
        self.frameLeftColumn.setMinimumWidth(self.settings["left_column_size"]["minimum"])
        self.frameLeftColumn.setStyleSheet(f"background: {self.themes['app_color']['bg_two']}")

        # 左列布局
        self.__m_layoutLeftColumn = QVBoxLayout(self.frameLeftColumn)
        self.__m_layoutLeftColumn.setContentsMargins(0, 0, 0, 0)

        # 左列
        self.leftColumn = ctQLeftColumn(parent,
                                        app_parent=self.widgetCentral,
                                        text_title="Settings Left Frame",
                                        text_title_size=self.settings["font"]["title_size"],
                                        text_title_color=self.themes['app_color']['text_foreground'],
                                        icon_path=Functions.f_getSvgIconPath("icon_settings.svg"),
                                        dark_one=self.themes['app_color']['dark_one'],
                                        bg_color=self.themes['app_color']['bg_three'],
                                        btn_color=self.themes['app_color']['bg_three'],
                                        btn_color_hover=self.themes['app_color']['bg_two'],
                                        btn_color_pressed=self.themes['app_color']['bg_one'],
                                        icon_color=self.themes['app_color']['icon_color'],
                                        icon_color_hover=self.themes['app_color']['icon_hover'],
                                        context_color=self.themes['app_color']['context_color'],
                                        icon_color_pressed=self.themes['app_color']['icon_pressed'],
                                        icon_close_path=Functions.f_getSvgIconPath("icon_close.svg")
                                        )
        # 左列布局添加控件
        self.__m_layoutLeftColumn.addWidget(self.leftColumn)
        # </editor-fold>

        # <editor-fold desc="右侧应用程序(app)">
        # <editor-fold desc="自定义标题栏">
        # 标题栏框架 & 布局
        self.frameTitleBar = QFrame()
        self.frameTitleBar.setMinimumHeight(40)
        self.frameTitleBar.setMaximumHeight(40)
        self.__m_layoutTitleBar = QVBoxLayout(self.frameTitleBar)
        self.__m_layoutTitleBar.setContentsMargins(0, 0, 0, 0)

        # 添加自定义标题栏
        self.titleBar = ctQTitleBar(parent,
                                    logo_width=120,
                                    app_parent=self.widgetCentral,
                                    logo_image="cosmo.svg",
                                    bg_color=self.themes["app_color"]["bg_two"],
                                    div_color=self.themes["app_color"]["bg_three"],
                                    btn_bg_color=self.themes["app_color"]["bg_two"],
                                    btn_bg_color_hover=self.themes["app_color"]["bg_three"],
                                    btn_bg_color_pressed=self.themes["app_color"]["bg_one"],
                                    icon_color=self.themes["app_color"]["icon_color"],
                                    icon_color_hover=self.themes["app_color"]["icon_hover"],
                                    icon_color_pressed=self.themes["app_color"]["icon_pressed"],
                                    icon_color_active=self.themes["app_color"]["icon_active"],
                                    context_color=self.themes["app_color"]["context_color"],
                                    dark_one=self.themes["app_color"]["dark_one"],
                                    text_foreground=self.themes["app_color"]["text_foreground"],
                                    radius=8,
                                    font_family=self.settings["font"]["family"],
                                    title_size=self.settings["font"]["title_size"],
                                    is_custom_title_bar=self.settings["custom_title_bar"]
                                    )
        self.__m_layoutTitleBar.addWidget(self.titleBar)

        # </editor-fold>

        # <editor-fold desc="右侧应用程序(app)框架&布局">
        # 右侧应用程序(app)框架
        self.frameRightApp = QFrame()

        # 右侧应用程序(app)布局
        self.__m_layoutRightApp = QVBoxLayout(self.frameRightApp)
        self.__m_layoutRightApp.setContentsMargins(3, 3, 3, 3)
        self.__m_layoutRightApp.setSpacing(6)
        # </editor-fold>

        # <editor-fold desc="右侧应用程序(app)主页内容">
        # 右侧应用程序(app)主页内容框架
        self.frameContentArea = QFrame()

        # 右侧应用程序(app)主页内容布局
        self.__m_layoutContentArea = QHBoxLayout(self.frameContentArea)
        self.__m_layoutContentArea.setContentsMargins(0, 0, 0, 0)
        self.__m_layoutContentArea.setSpacing(0)

        # 左侧内容框架
        self.frameContentAreaLeft = QFrame()

        # 从ui文件中加载主页内容
        self.mainPages = ctQMainPages()
        self.mainPages.setupUi(self.frameContentAreaLeft)

        # 右列框架
        self.frameRightColumn = QFrame()
        self.frameRightColumn.setMinimumWidth(self.settings["right_column_size"]["minimum"])
        self.frameRightColumn.setMaximumWidth(self.settings["right_column_size"]["minimum"])

        # 右列布局
        self.__m_layoutContentAreaRight = QVBoxLayout(self.frameRightColumn)
        self.__m_layoutContentAreaRight.setContentsMargins(5, 5, 5, 5)
        self.__m_layoutContentAreaRight.setSpacing(0)

        # 右列框架
        self.frameContentAreaRight = QFrame()
        self.frameContentAreaRight.setObjectName("content_area_right_bg_frame")
        self.frameContentAreaRight.setStyleSheet(f'''
        #content_area_right_bg_frame {{
            border-radius: 8px;
            background-color: {self.themes["app_color"]["bg_two"]};
        }}
        ''')

        # 右列布局中添加右列框架
        self.__m_layoutContentAreaRight.addWidget(self.frameContentAreaRight)

        # 右列
        self.rightColumn = ctQRightColumnUI()
        self.rightColumn.setupUi(self.frameContentAreaRight)

        # 右侧应用程序(app)主页内容布局添加左侧内容框架、右列框架
        self.__m_layoutContentArea.addWidget(self.frameContentAreaLeft)
        self.__m_layoutContentArea.addWidget(self.frameRightColumn)
        # </editor-fold>

        # <editor-fold desc="作者版本工具栏框架&布局">
        # 框架
        self.frameCopyRightVersionBar = QFrame()
        self.frameCopyRightVersionBar.setMinimumHeight(26)
        self.frameCopyRightVersionBar.setMaximumHeight(26)

        # 布局
        self.__m_layoutCopyRightVersionBar = QVBoxLayout(self.frameCopyRightVersionBar)
        self.__m_layoutCopyRightVersionBar.setContentsMargins(0, 0, 0, 0)

        # 控件
        self.copyRightVersionBar = ctQCopyRightVersionBar(bg_two=self.themes["app_color"]["bg_two"],
                                                          copyright=self.settings["copyright"],
                                                          version=self.settings["version"],
                                                          font_family=self.settings["font"]["family"],
                                                          text_size=self.settings["font"]["text_size"],
                                                          text_description_color=self.themes["app_color"][
                                                              "text_description"]
                                                          )

        #  布局添加控件
        self.__m_layoutCopyRightVersionBar.addWidget(self.copyRightVersionBar)
        # </editor-fold>

        # <editor-fold desc="右侧应用程序(app)布局添加标题栏、右侧应用程序(app)主页内容框架、作者版本工具栏">
        self.__m_layoutRightApp.addWidget(self.frameTitleBar)
        self.__m_layoutRightApp.addWidget(self.frameContentArea)
        self.__m_layoutRightApp.addWidget(self.frameCopyRightVersionBar)
        # </editor-fold>
        # </editor-fold>

        # <editor-fold desc="窗体布局中添加左侧菜单栏、左列、右侧应用程序(app)">
        self.window.layout.addWidget(self.frameLeftMenuBar)
        self.window.layout.addWidget(self.frameLeftColumn)
        self.window.layout.addWidget(self.frameRightApp)
        # </editor-fold>

        # 设置窗体的父控件
        parent.setCentralWidget(self.widgetCentral)
